package cn.wolfcode.shop.seckill.web.frontend;

import cn.wolfcode.cloud.redis.RedisService;
import cn.wolfcode.cloud.result.Result;
import cn.wolfcode.shop.member.domain.User;
import cn.wolfcode.shop.seckill.domain.OrderInfo;
import cn.wolfcode.shop.seckill.domain.SeckillOrder;
import cn.wolfcode.shop.seckill.mq.MQConstants;
import cn.wolfcode.shop.seckill.mq.OrderMessage;
import cn.wolfcode.shop.seckill.redis.SeckillKeyPrefix;
import cn.wolfcode.shop.seckill.result.SeckillGoodServerCodeMsg;
import cn.wolfcode.shop.seckill.service.IOrderService;
import cn.wolfcode.shop.seckill.service.ISeckillGoodService;
import cn.wolfcode.shop.seckill.service.ISeckillOrderService;
import cn.wolfcode.shop.seckill.vo.SeckillGoodVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/seckill")
public class SeckillOrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private ISeckillOrderService seckillOrderService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //分段锁的标记
    public static ConcurrentHashMap<Long,Boolean> lock = new ConcurrentHashMap();

    @RequestMapping("/{path}/doSeckill")
    public Result<String>  doSeckill(Long goodId,User user,String uuid){
        //秒杀订单的业务处理逻辑
        //1 当前用户是否登录
        if(user==null){
            return Result.error(SeckillGoodServerCodeMsg.USER_EMPTY);
        }
        //判断地址是否正确
        String oldPath = redisService.get(SeckillKeyPrefix.SECKILLPATH, user.getId()+":"+goodId, String.class);
        if(StringUtils.isEmpty(oldPath)){
            return Result.error(SeckillGoodServerCodeMsg.SG_ERROR);
        }
        //判断缓存是否存在
        Boolean flag = lock.get(goodId);
        if(flag != null && flag){
            return Result.error(SeckillGoodServerCodeMsg.SG_ERROR);
        }
        //2 查询商品是否存在
        SeckillGoodVo seckillGoodVo = seckillGoodService.findById(goodId);
        if(seckillGoodVo==null){
            return Result.error(SeckillGoodServerCodeMsg.SECKILL_GOOD_EMPTY);
        }
        //3 判断当前是否在抢购活动中  秒杀商品的开始时间必须 < 当前时间
        if(seckillGoodVo.getStartDate().getTime()-new Date().getTime()>0){
            return Result.error(SeckillGoodServerCodeMsg.TIME_OVER);
        }
        //5 当前用户是否重复下单操作
        OrderInfo orderInfo = redisService.get(SeckillKeyPrefix.SECKILLGOODORDER,user.getId()+":"+goodId,OrderInfo.class);
        if(orderInfo!=null){
            return Result.error(SeckillGoodServerCodeMsg.REPAY_ORDER_ERROR);
        }
        //4 判断库存是否充足 > 0
        Long num = redisService.decr(SeckillKeyPrefix.SECKILLGOODSTOCK, goodId + "");
        if(num < 0){
            lock.put(goodId,true);
            return Result.error(SeckillGoodServerCodeMsg.SG_ERROR);
        }
        /*try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        // 6 开始下单操作
        /*String orderNo = seckillOrderService.doSeckill(seckillGoodVo,user.getId());*/
        OrderMessage orderMessage = new OrderMessage();
        orderMessage.setGoodId(goodId);
        orderMessage.setUserId(user.getId());
        orderMessage.setUuid(uuid);
        rabbitTemplate.convertAndSend("", MQConstants.ORDER_PEDDING_QUEUE,orderMessage);
        return Result.success("正在抢购中....");
    }

    @RequestMapping("/findSeckillOrderByNo")
    public Result<OrderInfo> findSeckillOrderByNo(String orderNo){
        OrderInfo orderInfo  =orderService.findByNo(orderNo);
        return  Result.success(orderInfo);
    }

}
